package com.tedu.weibo.controller;

import com.tedu.weibo.common.result.JsonResult;
import com.tedu.weibo.common.result.StatusCode;
import com.tedu.weibo.mapper.WeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDatailVO;
import com.tedu.weibo.pojo.vo.WeiboVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo")
@Slf4j
@Validated
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @ApiOperation("发表微博")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.info("WeiboController.insert(),发表微博");
        log.trace("开始执行insert方法");
        log.debug("weiboDTO:"+weiboDTO);
        User user = (User) session.getAttribute("user");
        log.trace("通过session获取用户登录信息");
        log.debug("user:"+user);
        if(user == null){
            log.trace("user为null,未登录");
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else {
            log.trace("user不为null,处理并发表微博");
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO, weibo);
            weibo.setCreated(new Date());
            weibo.setUserId(user.getId());
            weiboMapper.insert(weibo);
            log.trace("插入微博完毕");
            return JsonResult.suceess();
        }
    }
    @ApiOperation("获取微博列表")
    @GetMapping("/selectIndex")
    public JsonResult selectIndex(){
//        List<WeiboVO> list = weiboMapper.selectIndex();
//        list.forEach(System.out::println);
        return JsonResult.suceess(weiboMapper.selectIndex());
    }
    @ApiOperation("获取微博详情")
    @ApiImplicitParam(name="id",value = "微博id",required = true,dataType = "int",example = "1")
    @GetMapping("/selectById")
    public JsonResult selectById(@Range(min=1,max=9999,message = "微博ID必须在1-9999之间") int id){
//        WeiboDatailVO weiboDatailVO = weiboMapper.selectById(id);
//        System.out.println(id);
        return JsonResult.suceess(weiboMapper.selectById(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "微博id",required = true,dataType = "int",example = "1"),
            @ApiImplicitParam(name="name",value = "用户昵称",required = true,dataType = "String",example = "张三")
    })
    @GetMapping("/selectByIdandName")
    public WeiboDatailVO selectByIdandName(int id,String name){
        return weiboMapper.selectById(id);
    }

}
